package com.xueyi.aixihu;

import com.xueyi.aixihu.config.SpringContextConfig;
import com.xueyi.aixihu.entity.po.AxhFinance;
import com.xueyi.aixihu.entity.po.AxhFinanceLog;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.OperationFinanceMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 描述 启动类1  111 niubi
 *
 * @auther 俞烨
 * @date 2022-08-13 09
 */
@SpringBootApplication
@EnableAspectJAutoProxy
@EnableScheduling
@MapperScan({"com.xueyi.aixihu.mapper", "com.xueyi.aixihu.entity.generatedMapper"})
@PropertySource("classpath:system.properties")
public class AxhApp {


    public static void main(String[] args) throws SzException, ParseException {
        SpringContextConfig.setSpringContent(SpringApplication.run(AxhApp.class));


        //修改指定年份下的报表情况（注意：输入月份个位数+0 如：01、02）
        //updateFinanceLogDate("2021");
    }

    //更新指定日期（年月）下报表日志时间信息
    public static void updateFinanceLogDate(String year){
        updateFinanceLogDate(year,null);
    }
    //更新指定日期（年月）下报表日志时间信息
    public static void updateFinanceLogDate(String year,String month){

        Calendar instance1 = Calendar.getInstance();
        Calendar instance2 = Calendar.getInstance();

        /**
         * 修改报表审核日志的审核时间（报表日期的月份+1）
         * 1：查询指定年份报表
         * 2：查询报表下相关审核日志
         * 3：将日志对应报表时间（年月）+1一个月份设置到审核日期时间
         */
        OperationFinanceMapper operationFinanceMapper = SpringContextConfig.getBean(OperationFinanceMapper.class);

        //1：查询指定年份报表
        List<AxhFinance> axhFinances = operationFinanceMapper.selectFinanceByYearOrMonth(StringUtils.isEmpty(month)?1:2,StringUtils.isEmpty(month)?year:year+"-"+month);
        if(!CollectionUtils.isEmpty(axhFinances)){

            //获取每一个报表日期
            Map<Integer, Date> financeDateMap = axhFinances.stream().collect(Collectors.toMap(AxhFinance::getId, AxhFinance::getCreateTime, (t1, t2) -> t1));

            //获取报表id
            List<Integer> financeIds = axhFinances.stream().map(AxhFinance::getId).collect(Collectors.toList());
//            financeIds.clear();
//            financeIds.add(187);

            //2：查询报表下相关审核日志
            List<AxhFinanceLog> axhFinanceLogs = operationFinanceMapper.selectFinanceLogBytFinanceId(financeIds);

            //将日志对应报表时间（年月）+1一个月份设置到审核日期时间
            for(AxhFinanceLog axhFinanceLog:axhFinanceLogs){

                //获取报表信息时间
                instance1.setTime(financeDateMap.get(axhFinanceLog.getFinanceId()));
                instance1.add(Calendar.MONTH,+1);

                //设置报表日志信息
                instance2.setTime(axhFinanceLog.getCreateTime());
                instance2.set(Calendar.YEAR,instance1.get(Calendar.YEAR));
                instance2.set(Calendar.MONTH,instance1.get(Calendar.MONTH));
                axhFinanceLog.setCreateTime(instance2.getTime());
            }

            //批量修改日志
            operationFinanceMapper.batchUpdateFinanceLog(axhFinanceLogs);
        }
    }

}
